<template>
  <div class="app-container agents-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="代理商名称" prop="agentsname">
        <el-input v-model="state.queryParams.wxpz_wxname" placeholder="代理商名称" clearable
          @keyup.enter="handleQuery({ initPageIndex: true })" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery({ initPageIndex: true })">搜索</el-button>
        <!-- <el-button type="primary" icon="Plus" plain @click="agentsExamineRef.openDialog()">新增</el-button> -->
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table v-loading="state.tableLoading" :data="state.tableData" row-key="agentsid" style="flex:1;">
      <el-table-column prop="agentsid" label="代理商id" min-width="200"></el-table-column>
      <el-table-column prop="agentsname" label="代理商名称" min-width="170" show-overflow-tooltip></el-table-column>
      <el-table-column prop="agentslxr" label="代理商联系人" min-width="120"></el-table-column>
      <el-table-column prop="agentslxdh" label="代理商联系电话" min-width="140"></el-table-column>
      <el-table-column prop="agentslxdz" label="代理商联系地址" min-width="150" show-overflow-tooltip></el-table-column>
      <el-table-column prop="agentsprice" label="代理商账户金额" min-width="120">
        <template #default="scope">
          <span>{{ Number(scope.row[scope.column.property]).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="agentsshstatus" label="代理商审核状态" min-width="120">
        <template #default="scope">
          <el-tag type="warning" v-if="scope.row[scope.column.property] === '待审核'">{{ scope.row[scope.column.property]
          }}</el-tag>
          <el-tag type="danger" v-if="scope.row[scope.column.property] === '审核不通过'">{{ scope.row[scope.column.property]
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="agentscreateuser" label="代理商创建人" min-width="150"></el-table-column>
      <el-table-column prop="agentscreatedate" label="代理商创建时间" min-width="170"></el-table-column>
      <el-table-column label="操作" :width="fitWidth" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="agentsExamineRef.openDialog(scope.row)">审核</el-text>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-box">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="paginaionConfig.query.pageindex" :page-sizes="paginaionConfig.sizeOptions"
        :page-size="paginaionConfig.query.rows" layout="total, sizes, prev, pager, next, jumper"
        :total="paginaionConfig.total">
      </el-pagination>
    </div>

    <AgentsExamine width="70%" ref="agentsExamineRef" @refresh="handleQuery" />
  </div>
</template>

<script setup name="Agents">
import useAgentsManageManageStore from '@store/modules/agentsManage'
import AgentsExamine from '../components/AgentsExamine/index.vue'
import _ from 'lodash'
import usePagination from '@/hooks/usePagination'
import useFitColumn from '@/hooks/useFitColumn'

const { proxy } = getCurrentInstance()
const agentsManageStore = useAgentsManageManageStore()
const { paginaionConfig, handleSizeChange, handleCurrentChange } = usePagination(handleQuery)
const { fitWidth } = useFitColumn()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    agentsshstatus: '待审核',
    agentsname: ''
  },
})
const dictsData = ref(proxy.useDict('关联公司'))
const agentsExamineRef = ref(null)

provide('dictsDataParent', readonly(dictsData))

/** 查询菜单列表 */
const getList = async (query) => {
  state.tableLoading = true
  const { Total, Data } = await agentsManageStore.list(query)
  paginaionConfig.total = Total
  state.tableData = Data

  state.tableLoading = false
}

function handleQuery(config) {
  if (config?.initPageIndex) paginaionConfig.query.pageindex = 1
  getList(Object.assign(_.cloneDeep(paginaionConfig.query), state.queryParams))
}

const availableChange = async (row, newVal) => {
  row.loading = true
  try {
    // await agentsManageStore.sysuserUp({ account: row.account, available: newVal })
    // row.loading = false
    // proxy.$modal.msgSuccess('操作成功！')
  } catch (error) {
    handleQuery()
  }
}

onMounted(() => {
  handleQuery({ initPageIndex: true })
})
</script>
